<?php

class cargo_m3 extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'cargo_co':
	 * @var integer $id
	 * @var integer $do_id
	 * @var integer $company_id
	 * @var integer $type
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cargo_m3';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('length, width, height, m3', 'numerical'),
                        array('id','numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'company'=>array(self::BELONGS_TO,'company','company_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
        public function saveM3() {
            $criteria = new CDbCriteria;
            $returnID = 0;
            if((!empty($this->length) || $this->length != 0) && (!empty($this->height) || $this->height != 0) && (!empty($this->width) || $this->width != 0)) {
                $criteria->condition = "length=:l AND width=:w AND height=:h";
                $criteria->params=array(':l'=>$this->length,':h'=>$this->height,':w'=>$this->width);
            }
            else {
                $criteria->condition = "length=:l AND width=:w AND height=:h AND m3=:m3";
                $criteria->params=array(':l'=>$this->length,':h'=>$this->height,':w'=>$this->width,'m3'=>$this->m3);
            }
            if($this->exists($criteria))
                $returnID = $this->find($criteria)->id;
            else {
                if(!$this->save())
                    return false;
                $returnID = $this->id;
            }
            return $returnID;

        }
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'do_id' => 'Do',
			'company_id' => 'C/O',
			'type' => 'Type',
		);
	}
}